package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.ZhangTingJuJiao;
import com.twang.stock.entity.ZhangTingJuJiaoCountDetail;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.ZhangTingJuJiaoMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.ZhangTingjuJiaoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * 涨停聚焦
 */

@Service
@Slf4j
public class ZhangTingJuJiaoTask extends IndexedInfo implements IndexDataTask {

    public static String ZHANGTINGJUJIAO_KEY_DX_GD="ZHANGTINGJUJIAO_KEY_DX_GD";

    public static String ZHANGTINGJUJIAO_KEY_DX_HX="ZHANGTINGJUJIAO_KEY_DX_HX";

    public static String DX_QX_GD="DX_QX_GD";//短线情绪高度

    public static String DX_QX_HX="DX_QX_HX";//短线核心


    //涨停封板率
    public static String ZHANGTINGJUJIAO_KEY_LIMIT_UP="ZHANGTINGJUJIAO_KEY_LIMIT_UP";

    //跌停数据
    public static String ZHANGTINGJUJIAO_KEY_LIMIT_DOWN="ZHANGTINGJUJIAO_KEY_LIMIT_DOWN";

    public static String DX_QX_LIMIT_UP="DX_QX_LIMIT_UP";

    public static String DX_QX_LIMIT_DOWN="DX_QX_LIMIT_DOWN";


    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private ZhangTingJuJiaoMapper zhangTingJuJiaoMapper;

    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) throws Exception {
        try {
            {
                List<String> dates = companyStockMinutesMapper.queryDates();
                ZhangTingJuJiao param=new ZhangTingJuJiao();
                param.setDate(dates.get(dates.size()-1));
                List<ZhangTingJuJiao> zhangTingJuJiaoList= zhangTingJuJiaoMapper.select(param);
                if(CollectionUtils.isNotEmpty(zhangTingJuJiaoList)){
                    //1-涨停版
                    List<ZhangTingJuJiao> 涨停池=zhangTingJuJiaoList.stream().filter(p->p.getOrderType().equals(ZhangTingjuJiaoService.涨停池_)).collect(Collectors.toList());
                    modelAndView.addObject("zhangTingBanList_ZT", 涨停池);

                    //2-连扳池
                    List<ZhangTingJuJiao> 连扳池=zhangTingJuJiaoList.stream().filter(p->p.getOrderType().equals(ZhangTingjuJiaoService.连扳池_)).collect(Collectors.toList());
                    modelAndView.addObject("zhangTingBanList_LBC", 连扳池);

                    //3-炸板池
                    List<ZhangTingJuJiao> 炸板池=zhangTingJuJiaoList.stream().filter(p->p.getOrderType().equals(ZhangTingjuJiaoService.炸板池_)).collect(Collectors.toList());
                    modelAndView.addObject("zhangTingBanList_ZBC", 炸板池);


                    //4-冲刺涨停
                    List<ZhangTingJuJiao> 冲刺涨停=zhangTingJuJiaoList.stream().filter(p->p.getOrderType().equals(ZhangTingjuJiaoService.冲刺涨停_)).collect(Collectors.toList());
                    modelAndView.addObject("zhangTingBanList_CCZT", 冲刺涨停);

                    //5-跌停池
                    List<ZhangTingJuJiao> 跌停池=zhangTingJuJiaoList.stream().filter(p->p.getOrderType().equals(ZhangTingjuJiaoService.跌停池_)).collect(Collectors.toList());
                    modelAndView.addObject("zhangTingBanList_DTC", 跌停池);


                }
                try {
                    ZhangTingJuJiaoCountDetail DX_QX_LIMIT_UP_ =JSONObject.parseObject(redisUtil.get(ZHANGTINGJUJIAO_KEY_LIMIT_UP).toString(), ZhangTingJuJiaoCountDetail.class);
                    if(null!=DX_QX_LIMIT_UP_.getToday().getRate()){
                        DX_QX_LIMIT_UP_.getToday().setRate(new BigDecimal(DX_QX_LIMIT_UP_.getToday().getRate()).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP).toString()+"%");
                    }else{
                        DX_QX_LIMIT_UP_.getToday().setRate("--");
                    }

                    if(null!=DX_QX_LIMIT_UP_.getYesterday().getRate()){
                        DX_QX_LIMIT_UP_.getYesterday().setRate(new BigDecimal(DX_QX_LIMIT_UP_.getYesterday().getRate()).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP).toString()+"%");
                    }else{
                        DX_QX_LIMIT_UP_.getYesterday().setRate("--");
                    }
                    modelAndView.addObject(DX_QX_LIMIT_UP, DX_QX_LIMIT_UP_);

                    //
                    ZhangTingJuJiaoCountDetail DX_QX_LIMIT_DOWN_ =JSONObject.parseObject(redisUtil.get(ZHANGTINGJUJIAO_KEY_LIMIT_DOWN).toString(), ZhangTingJuJiaoCountDetail.class);
                    if(null!=DX_QX_LIMIT_DOWN_.getToday().getRate()){
                        DX_QX_LIMIT_DOWN_.getToday().setRate(new BigDecimal(DX_QX_LIMIT_DOWN_.getToday().getRate()).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP).toString()+"%");
                    }else{
                        DX_QX_LIMIT_DOWN_.getToday().setRate("--");
                    }

                    if(null!=DX_QX_LIMIT_DOWN_.getYesterday().getRate()){
                        DX_QX_LIMIT_DOWN_.getYesterday().setRate(new BigDecimal(DX_QX_LIMIT_DOWN_.getYesterday().getRate()).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP).toString()+"%");
                    }else{
                        DX_QX_LIMIT_DOWN_.getYesterday().setRate("--");
                    }
                    modelAndView.addObject(DX_QX_LIMIT_DOWN,DX_QX_LIMIT_DOWN_);

                }catch (Exception e){
                    log.error(">>>>>>>>>> erro {}",e);
                }
                modelAndView.addObject(DX_QX_GD, redisUtil.get(ZHANGTINGJUJIAO_KEY_DX_GD));
                modelAndView.addObject(DX_QX_HX, redisUtil.get(ZHANGTINGJUJIAO_KEY_DX_HX));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("涨停聚焦 执行完成，当前剩余条数:{}", latch.getCount());
            latch.countDown();
        }
    }
}
